/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.mcx.modules.service.impl;

import com.mcx.modules.domain.Gameserverconfig;
import lombok.RequiredArgsConstructor;
import com.mcx.modules.repository.GameserverconfigRepository;
import com.mcx.modules.service.GameserverconfigService;
import com.mcx.modules.service.dto.GameserverconfigDto;
import com.mcx.modules.service.dto.GameserverconfigQueryCriteria;
import com.mcx.modules.service.mapstruct.GameserverconfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.mcx.utils.PageUtil;
import com.mcx.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author mcx
* @date 2022-04-30
**/
@Service
@RequiredArgsConstructor
public class GameserverconfigServiceImpl implements GameserverconfigService {

    private final GameserverconfigRepository gameserverconfigRepository;
    private final GameserverconfigMapper gameserverconfigMapper;

    @Override
    public Map<String,Object> queryAll(GameserverconfigQueryCriteria criteria, Pageable pageable){
        Page<Gameserverconfig> page = gameserverconfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(gameserverconfigMapper::toDto));
    }

    @Override
    public List<GameserverconfigDto> queryAll(GameserverconfigQueryCriteria criteria){
        return gameserverconfigMapper.toDto(gameserverconfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public GameserverconfigDto findById(Integer id) {
        Gameserverconfig gameserverconfig = gameserverconfigRepository.findById(id).orElseGet(Gameserverconfig::new);
        return gameserverconfigMapper.toDto(gameserverconfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GameserverconfigDto create(Gameserverconfig resources) {
        return gameserverconfigMapper.toDto(gameserverconfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Gameserverconfig resources) {
        Gameserverconfig gameserverconfig = gameserverconfigRepository.findById(resources.getId()).orElseGet(Gameserverconfig::new);
        gameserverconfig.copy(resources);
        gameserverconfigRepository.save(gameserverconfig);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            gameserverconfigRepository.deleteById(id);
        }
    }

    @Override
    public List<GameserverconfigDto> queryAll() {
        return gameserverconfigMapper.toDto(gameserverconfigRepository.findAll());
    }
}